package com.wangmei.controller;

import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangmei.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.spring.web.json.Json;
import unitTool.Dictionary;
import unitTool.JsonRsp;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/myarea")
public class MyCollectionController {
    @Autowired
    CollectionService collectionService;
    @RequestMapping(value = "/myCollection",method = RequestMethod.POST)
    public JsonRsp getMyCollection(@RequestParam(value = "uid")int uid
            ,@RequestParam(value = "pageIndex",defaultValue = Dictionary.PageDirect.PAGE_INDEX)
                                               int pageIndex
            ,@RequestParam(value = "pageSize", defaultValue = Dictionary.PageDirect.PAGE_SIZE)
                                               int pageSize ){
        try{
            PageHelper.startPage(pageIndex,pageSize);
            PageInfo page = new PageInfo(collectionService.getMyCollection(uid));
            return JsonRsp.success(page.getList()).setTotal(page.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            return JsonRsp.fail(1000,"系统内部错误");
        }
    }

    @RequestMapping(value = "/changeModel",method = RequestMethod.GET)
    public JsonRsp changeModel(
            @RequestParam(value = "uid")int uid,
            @RequestParam(value = "pid")int pid){
        try{
            Map<String,Object> map = new HashMap<>();
            map.put("model",collectionService.changeType(uid,pid));
            return JsonRsp.success(map,"success");
        }catch (Exception e){
            e.printStackTrace();
            return JsonRsp.fail(100,"内部错误");
        }
    }
}
